<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-13
 * Time: 下午11:35
 */
class Common extends CI_Controller
{
    function __construct() {
        parent::__construct ();
        $this->load->database ();
        $this->checkLogin();
        $this->initUser();
    }

    function checkLogin()
    {
        $uid = isset($this->session->userdata['id']) ? $this->session->userdata['id'] :0;

        //$mod = array('login');
        $noeAct = array(
            'login',
            'registere',
            'registerp',
            'loginemail',
            'index',
            'logon',
            'star',
            'star_content',
            'intro',
            'apply',
            'addApply',
            'about',
            'clause',
            'contact',
            'news',
            'join',
            'info',
            'madouhelp',
            'sendSms',
            'checkPhoneForRegister',
            'checkEmailForRegister',
            'registerEmailSubmit',
            'registeractivate',
            'checkSmsCode',
            'registerpsubmit',
            'girl',
            'showperson',
            'photoWall',
            'bumshow',
            'tinyInterview',
            'ajaxVote',
            'ajaxSupport'
        );//nologin
        $yesAct = array(
            'login',
            'registere',
            'registerp',
            'index',
            );//yeslogin

        //由于前一个程序员把登陆的后台与前台session设置的一样只能管理员访问前台时注销掉其后台的登陆状态
        if(isset($this->session->userdata['isvalid']) && $this->session->userdata['isvalid'] == 1){
            unset($this->session);
        }

        if(empty($uid) && !in_array($this->router->method,$noeAct) ){

            $jumpUrl = base_url()."index.php/login/login";
            redirect($jumpUrl);
        }elseif(!empty($uid) && in_array($this->router->method,$yesAct)){
            $jumpUrl = base_url()."index.php/login/show";
            redirect($jumpUrl);
        }


    }

    function initUser()
    {
        $uid = isset($this->session->userdata['id'])?(int)$this->session->userdata['id']:'';
        if($uid > 0 && empty($this->session->userdata['userinfo'])){
            $query = $this->Muser->getUserByUid ($uid);
            $userinfo['email'] = $query['email'];
            $userinfo['realname'] = $query['realname'];
            $userinfo['phone'] = $query['phone'];
            $this->session->set_userdata ( 'userinfo', $userinfo);
        }
       // print_r($this->session->userdata);
    }
} 